﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Zj.CourseManage.DTO.Common.Pagination
{
    /// <summary>
    /// page list class
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PaginatedList<T>:List<T>
    {
        /// <summary>
        /// page index
        /// </summary>
        public int PageIndex { get; set; }



        /// <summary>
        /// page total count
        /// </summary>
        public int PageCount { get; set; }


        /// <summary>
        /// has previous page?
        /// </summary>
        public bool HasPrevious { get { return PageIndex > 1; } }



        /// <summary>
        /// has next page?
        /// </summary>
        public bool HasNext { get { return PageIndex < PageCount; } }




        public PaginatedList(List<T> items, int total,int pageIndex,int pageSize)
        {
            this.PageIndex = pageIndex;
            this.PageCount = Convert.ToInt32(Math.Ceiling(total / (decimal)pageSize));
            this.AddRange(items);
        }



        /// <summary>
        /// create page list
        /// </summary>
        /// <param name="source">data source</param>
        /// <param name="pageIndex">page index</param>
        /// <param name="pageSize">count per page</param>
        /// <returns></returns>
        public static  PaginatedList<T> CreatePaging(IQueryable<T> source,int pageIndex,int pageSize)
        {
            var items = source.Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            return new PaginatedList<T>(items, source.Count(),pageIndex, pageSize);
        }
    }
}
